package com.xxxx.yoyo.adminweb;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xxxx.yoyo.bean.Goods;
import com.xxxx.yoyo.bean.Type;
import com.xxxx.yoyo.service.IGoodsService;
import com.xxxx.yoyo.service.ITypeService;
import com.xxxx.yoyo.service.impl.GoodsServiceImpl;
import com.xxxx.yoyo.service.impl.TypeServiceImpl;

@WebServlet("/admin/typeDelete")
public class TypeDeleteServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private ITypeService typeService = new TypeServiceImpl();
	private IGoodsService goodsService = new GoodsServiceImpl();
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获取前端页面传递过来的参数id
		String typeIdstr = request.getParameter("id");
		int typeId = Integer.parseInt(typeIdstr);
		//根据宠物分类的id查询分类信息
		Type type = typeService.getTypeById(typeId);
		
		//查看宠物表中是否有宠物属于要被删除的分类.
		List<Goods> goodsList = goodsService.getGoodsListByTypeId(typeId);
		if(goodsList.size() == 0){//说明这个宠物分类下没有宠物.---走正常的删除流程
			//真实开发中，不会真删数据，而是逻辑删除（标记删除）
			type.setStatus(0);//0表示删除1表示正常
			typeService.updateType(type);
			//提示成功信息
			request.setAttribute("msg","删除["+type.getName()+"]宠物大类成功！");
		}else{//说明这个宠物分类下有宠物----直接告知删除失败
			//提示失败信息
			request.setAttribute("msg","删除["+type.getName()+"]宠物大类失败！该宠物大类下有宠物，不能删除这个宠物大类。");
		}
		//设置导航条上的[宠物大类管理]处于选中状态
		request.setAttribute("flag",4);
		//获取[宠物分类]列表
		List<Type> typeList = typeService.getTypeList();
		//把typeList存入request中，供/admin/type list.jsp页面使用
		request.setAttribute("typeList",typeList);
		
		//请求转发，跳转到/admin/type_list.jsp页面
		request.getRequestDispatcher("/admin/type_list.jsp").forward(request,response);
	
		}
}
